package demo2;

public class PeopleTest {
    public static void main(String[] args) {
        People[]arr=new People[3];
        People p1= new People(01,"张三",22);
        People p2= new People(02,"李四",23);
        People p3= new People(03,"五",23);
        arr[0]=p1;
        arr[1]=p2;
        arr[2]=p3;
        //要求3：通过id删除学生信息
        //    如果存在，则删除，如果不存在，显示删除失败。
        int index=getIndex(arr,4);
        if(index>=0){
            arr[index]=null;
            printArr(arr);
        }else {
            System.out.println("当前id不存在，删除失败");
        }
    }
    public static int getIndex(People[]arr,int id){
        for (int i = 0; i < arr.length; i++) {
            People peo = arr[i];
            if(peo!=null){
                int pid=peo.getId();
                if(pid==id){
                    return i;
                }
            }
        }return -1;
    }
    public static void printArr(People[]arr){
        for (int i = 0; i < arr.length; i++) {
             People p=arr[i];
            if(p!=null){
                System.out.println(p.getId()+" "+p.getName()+" "+p.getAge());
            }
        }
    }
}
